package com.test.deencredit.dialog_deencredit;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.test.deencredit.R;
import com.test.deencredit.activity_deencredit.FeedbackActivity_deencredit;
import com.test.deencredit.bean_deencredit.config_deencredit.AppShowCustomize_deencredit;
import com.test.deencredit.bean_deencredit.star_dialog_deencredit.ActiveWindowConfig_deencredit;
import com.test.deencredit.config_deencredit.Constant_deencredit;
import com.test.deencredit.analytics_deencredit.util_deencredit.ApRvUtil_deencredit;
import com.test.deencredit.util_deencredit.UserInfoUtil_deencredit;
import com.test.deencredit.util_deencredit.http_deencredit.HttpUtil_deencredit;

public class RateActiveDialog_deencredit extends Dialog {
    
    private final String url_deencredit = "https://play.google.com/store/apps/details?id=" + Constant_deencredit.PACKAGE_NAME;
    private int score_deencredit = 0;
    private static boolean isShow_deencredit = false;
    private final ImageButton[] starIbs_deencredit = new ImageButton[5];

    private final String page_deencredit = "DIALOG_REVIEW_ACTIVITE";

    private final AppCompatActivity activity_deencredit;

    public RateActiveDialog_deencredit(AppCompatActivity activity){
        super(activity);
        this.activity_deencredit = activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_rate_active_deencredit);

        setCancelable(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCanceledOnTouchOutside(false);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setBackgroundDrawableResource(R.color.transparent);


        setCancelable(false);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCanceledOnTouchOutside(false);

        findViewById(R.id.dra_btn_notnow).setOnClickListener(v -> {
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"CANCEL_CLICK",String.valueOf(score_deencredit));
            dismiss();
        });

        TextView textTv = findViewById(R.id.dra_tv_text);

        String content = ActiveWindowConfig_deencredit.getInstance().getContent_deencredit();
        if (!TextUtils.isEmpty(content)) textTv.setText(content);

        starIbs_deencredit[0] = findViewById(R.id.dra_ib_star1);
        starIbs_deencredit[1] = findViewById(R.id.dra_ib_star2);
        starIbs_deencredit[2] = findViewById(R.id.dra_ib_star3);
        starIbs_deencredit[3] = findViewById(R.id.dra_ib_star4);
        starIbs_deencredit[4] = findViewById(R.id.dra_ib_star5);

        findViewById(R.id.dra_btn_rate).setOnClickListener(v -> {

            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"RATE_CLICK",String.valueOf(score_deencredit));

            if (score_deencredit ==0) {
                Toast.makeText(activity_deencredit, activity_deencredit.getString(R.string.tips_please_rate_deencredit),Toast.LENGTH_SHORT).show();
                return;
            }

            if (score_deencredit >=4){
                if (AppShowCustomize_deencredit.getInstance_deencredit().showSysReview_deencredit()){
                    ApRvUtil_deencredit.INSTANCE.launchRvFlow_deencredit(activity_deencredit, aBoolean -> {
                        if (!aBoolean) activity_deencredit.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url_deencredit)));
                        dismiss();
                        return null;
                    });
                }else activity_deencredit.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url_deencredit)));


            }else activity_deencredit.startActivity(new Intent(activity_deencredit, FeedbackActivity_deencredit.class));

            dismiss();
        });

        for (int i = 0; i< starIbs_deencredit.length; i++){
            ImageButton scoreIb = starIbs_deencredit[i];
            final int fScore = i+1;
            if (scoreIb!=null) scoreIb.setOnClickListener(v -> setScore_deencredit(fScore));
        }

    }

    private void setScore_deencredit(int score_deencredit){

        if (score_deencredit >5) score_deencredit =5;
        else if (score_deencredit <1) score_deencredit =1;

        this.score_deencredit = score_deencredit;

        HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"STAR_CLICK",String.valueOf(score_deencredit));

        for (int i = 0; i< starIbs_deencredit.length; i++){
            ImageButton scoreIb = starIbs_deencredit[i];
            if (scoreIb==null) continue;
            scoreIb.setBackgroundResource(i< score_deencredit ? R.drawable.icon_rate_star_true_deencredit :R.drawable.icon_rate_star_false_deencredit);
        }
    }

    @Override
    public void show() {
        if (isShow_deencredit) return;
        isShow_deencredit = true;
        super.show();
        HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"START","");
        UserInfoUtil_deencredit.getInstance_deencredit().addActivedialogCount_deencredit();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"END","");
        isShow_deencredit = false;
    }
    
}
